package com.lagou.edu.filer;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: stage-3-module-3
 * @description:
 * @author: zhangxiaoyang
 * @date: 2020-09-01 06:11
 **/
@Activate(group = {CommonConstants.CONSUMER})
public class TransportIPFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        System.out.println("》》》》》》》》》执行过滤器");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        RpcContext.getContext()
                  .setAttachment("client-ip", request.getRemoteHost());
        System.out.println("设置IP：" + RpcContext.getContext()
                                               .getAttachment("client-ip"));
        return invoker.invoke(invocation);
    }
}
